function eq(obj, key, target) {
    if (obj === null || obj === undefined) {
        return false;
    }
    let val = analysisObjValue(obj, key)
    if (target === val) {
        return true;
    }
    if (target == val) {
        return true
    }
    let dataType = getDataType(target);
    if (!dataType) {
        throw new Error("未知数据类型")
    }
    switch (dataType) {
        case "boolean":
            if (val === 'true') {
                val = true;
            } else if (val === 'false') {
                val = false
            } else {
                val = null;
            }
            break
        case "number":
            val = Number(val);
            break
    }
    return val === target;
}

function ne(obj, key, target) {
    return !eq(obj, key, target)
}

function getDataType(val) {
    let dataType = typeof val
    if (dataType === 'object') {
        if (Array.isArray(val))
            return "array";
        return "object"
    }
    return dataType;
}

function analysisObjValue(obj, key) {
    let indexOf = key.indexOf(".");
    if (indexOf < 0) {
        return obj[key];
    }
    let nextKey = key.substring(indexOf + 1);
    let curKey = key.substring(0, indexOf);
    let tempVal = obj[curKey]
    return analysisObjValue(tempVal, nextKey);
}

function objPropMappingValue(obj, targetKey, mapperKey) {
    if (obj[targetKey]) {
        return
    }
    obj[targetKey] = obj[mapperKey]
}

function arrayItemMappingValue(array, targetKey, mapperKey) {
    for (const item of array) {
        objPropMappingValue(item, targetKey, mapperKey);
    }
}

const startPropTools = {
    eq,
    ne,
    objPropMappingValue,
    arrayItemMappingValue,
    analysisObjValue
}
export default startPropTools
